package com.bigzero.limit.system.controller;

import com.bigzero.limit.system._enum.LimitType;
import com.bigzero.limit.system.annotation.Limit;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.concurrent.atomic.AtomicInteger;

/**
 * 限流控制层实现
 *
 * @author fanxinxiong
 * @since 2023-01-30
 */
@RestController
public class LimiterController {
    private static final AtomicInteger ATOMIC_INTEGER_1 = new AtomicInteger();
    private static final AtomicInteger ATOMIC_INTEGER_2 = new AtomicInteger();
    private static final AtomicInteger ATOMIC_INTEGER_3 = new AtomicInteger();

    /**
     * @author fanxinxiong
     * @description
     * @date 2023-01-30
     */
    @GetMapping("/limitTest")
    public int testLimiter() {
        System.out.println("LimiterController 请求成功!");
        return 1;
    }

    /**
     * @author fanxinxiong
     * @description
     * @date 2023-01-30
     */
    @Limit(key = "limitTest", period = 10, count = 3)
    @GetMapping("/limitTest1")
    public int testLimiter1() {

        return ATOMIC_INTEGER_1.incrementAndGet();
    }

    /**
     * @author fanxinxiong
     * @description
     * @date 2023-01-30
     */
    @Limit(key = "customer_limit_test", period = 10, count = 3, limitType = LimitType.CUSTOMER)
    @GetMapping("/limitTest2")
    public int testLimiter2() {

        return ATOMIC_INTEGER_2.incrementAndGet();
    }

    /**
     * @author fanxinxiong
     * @description
     * @date 2023-01-30
     */
    @Limit(key = "ip_limit_test", period = 10, count = 3, limitType = LimitType.IP)
    @GetMapping("/limitTest3")
    public int testLimiter3() {

        return ATOMIC_INTEGER_3.incrementAndGet();
    }

}
